import 'package:flutter/material.dart';
import 'package:jiaju_flutter/widgets/image/comm_image.dart';

class InfoWidget extends StatelessWidget {
  final List<InfoItem> itemList;
  final String label;
  final Function(InfoItem item, int index) onTap;
  const InfoWidget(this.itemList, {this.label, this.onTap, Key key})
      : super(key: key);

  _buildHeader(BuildContext context) {
    if (label == null || label == "") return Container();
    return Container(
      margin: EdgeInsets.only(left: 10, top: 10),
      alignment: Alignment.topLeft,
      child: Text(
        label,
        style: TextStyle(fontWeight: FontWeight.bold),
      ),
    );
  }

  _buildContent(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 10, left: 10, right: 10),
      child: ListView.builder(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          itemCount: itemList.length,
          itemBuilder: (BuildContext context, int index) {
            var item = itemList[index];
            return Container(
              height: 70,
              margin: EdgeInsets.only(bottom: 10),
              child: Row(
                children: [
                  Container(
                    child: CommImage(
                      item.image,
                      width: 100,
                      height: 70,
                      fit: BoxFit.cover,
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(left: 10),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(item.title),
                          Text(item.time),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            );
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          _buildHeader(context),
          _buildContent(context),
        ],
      ),
    );
  }
}

class InfoItem {
  final String title;
  final String image;
  final String time;
  const InfoItem(this.title, this.image, this.time);
}
